<template>
  <el-upload   class="upload-demo" :accept="uploadType" :before-upload="beforeAvatarUpload" :action="uploadUrl" :file-list="fileList"
    :headers="{'Authorization':'Bearer ' + tokenUpload}"   :limit=1 :on-success="successUpload"
    :before-remove="beforeRemove" :on-remove="handleRemove" :on-exceed="handleExceed">
   <!-- <i class="el-icon-upload"></i>
    <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
    <div class="el-upload__tip" slot="tip"><span style="color:red">{{des}}</span></div> -->
     <el-button size="small" type="primary">点击上传</el-button>
      <div slot="tip" class="el-upload__tip">{{des}}</div>
  </el-upload>
</template>

<script>
  import {
    delDoc
  } from "@/api/kenuo/verison";
  import Cookies from "js-cookie";
  export default {
    name: "uploadFiles",
    props: ['fileList', "uploadType", "des"],
    data() {
      return {
        //文件上传地址
        // fileList: [],
        tokenUpload: '',
        uploadUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
      }
    },
    created() {
      console.log(process.env.VUE_APP_BASE_API)
      this.tokenUpload = Cookies.get('Admin-Token')
    },
    methods: {
      // errorUpload(err, file, fileList){
      //   console.log(err)
      //   console.log(file)
      //    console.log(fileList)
      // },
      //超过文件限制个数
      handleExceed() {
        this.$message.warning(`不要重复上传文件`);
      },
      //删除之前
      beforeRemove(file, fileList) {
        let a = true;
        if (file && file.status === "success") {
          a = this.$confirm(`确定删除 ${ file.name }？`);
        }
        return a;
      },
      //删除文件
      handleRemove(file, fileList) {
        if (file && file.status === "success") {
          delDoc({
            url: file.url
          }).then((res) => {
            if (res.code == 200) {
              // this.fileList = []
              this.$emit('operateFiles', [])
              this.$message({
                message: "删除成功",
                type: "success"
              });
            }
          });
        }
      },
      //上传成功
      successUpload(response, file, fileList) {

        if (response.code == 200) {
          this.$emit('operateFiles', [{
            name: response.originalFilename,
            url: response.url
          }])
          // this.fileList = [{
          //   name: response.originalFilename,
          //   url: response.url
          // }]
        } else if (response.code == 500) {
          this.$emit('operateFiles', [])
          this.$message({
            message: response.msg,
            type: "error"
          });
        }
      },
      beforeAvatarUpload(file) {
         if(file.type.indexOf('video') != -1){
            const isLt200M = file.size / 1024 / 1024 < 200;
           // if (['video/mp4', 'video/ogg','video/flv','video/avi','video/wmv','video/rmvb'].indexOf(file.type) == -1) {
           if (['video/mp4', 'video/flv'].indexOf(file.type) == -1) {
                  this.$message.error('上传视频只能是 mp4、flv格式!');
                  return false;
                }
                if (!isLt200M) {
                  this.$message.error('上传视频大小不能超过 200MB!');
                  return false;
                }
                return true;
         }else{
           const isLt200M = file.size / 1024 / 1024 < 200;
           if (!isLt200M) {
             this.$message.error('上传文件大小不能超过 200MB!');
             return false;
           }
            return true;
         }

      }
    }
  }
</script>

<style>
</style>
